package experiment5.task;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description: 连接服务器，启动接受端线程和发送端线程
 * @author:fyl
 * @date 2021/4/22 9:48
 * @Modified By:
 * @Modified Date:
 */
public class Client {
    public static void main(String[] args) {
        //从键盘上获取用户输入数据
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Socket socket = null;
        String name = null;
        try {
            System.out.println("输入你的用户名:");
            name = br.readLine();
            if ("".equals(name)) return;

            socket = new Socket("localhost", 8080);
        } catch (IOException e) {
            System.err.println("连接失败！");
            CloseUtil.closeAll(br);
            e.printStackTrace();
            System.exit(0);
        }

        //使用线程池启动两个线程，发送和接受
        ExecutorService pool = Executors.newFixedThreadPool(2);
        pool.submit(new ClientSend(socket, name));
        pool.submit(new ClientReceive(socket));

    }
}
